using Microsoft.EntityFrameworkCore;
using UwinEducation.Domain.Entities;
using UwinEducation.Domain.IRepositories;
using UwinEducation.Infrastructure.Persistence;
using UwinEducation.Shared.Enums;
using UwinEducation.Shared.Models;

namespace UwinEducation.Infrastructure.Repositories
{
    /// <summary>
    /// 预约仓储实现
    /// </summary>
    public class AppointmentRepository : Repository<Appointment, Guid>, IAppointmentRepository
    {
        public AppointmentRepository(ApplicationDbContext context) : base(context)
        {
        }

        public async Task<PagedResult<Appointment>> GetPagedListAsync(AppointmentPageRequest pageRequest)
        {
            var query = _context.Appointments.AsNoTracking()  // 提高性能
                .Include(a => a.Student)
                .Include(a => a.Teacher)
                .AsQueryable();

            if (pageRequest.StudentId.HasValue)
            {
                query = query.Where(a => a.StudentId == pageRequest.StudentId);
            }
            if (pageRequest.TeacherId.HasValue)
            {
                query = query.Where(a => a.TeacherId == pageRequest.TeacherId);
            }
            if (!string.IsNullOrWhiteSpace(pageRequest.StudentName))
            {
                query = query.Where(a => a.Student.Name.Contains(pageRequest.StudentName));
            }
            if (!string.IsNullOrWhiteSpace(pageRequest.StudentNo))
            {
                query = query.Where(a => a.Student.StudentNo.Contains(pageRequest.StudentNo));
            }

            if (pageRequest.Status.HasValue)
            {
                query = query.Where(a => a.Status == pageRequest.Status);
            }
            if (pageRequest.StartDate.HasValue)
            {
                query = query.Where(a => a.Deadline >= pageRequest.StartDate);
            }
            if (pageRequest.EndDate.HasValue)
            {
                query = query.Where(a => a.Deadline <= pageRequest.EndDate);
            }

            var total = await query.CountAsync();
            var items = await query.AsSplitQuery()  // 避免笛卡尔积
                .OrderByDescending(a => a.CreatedAt)
                .Skip((pageRequest.PageIndex - 1) * pageRequest.PageSize)
                .Take(pageRequest.PageSize)
                .ToListAsync();

            return new PagedResult<Appointment>
            {
                Total = total,
                Items = items
            };
        }

        public async Task<Appointment> GetDetailsByIdAsync(Guid id)
        {
            return await _context.Appointments
                .Include(a => a.Student)
                .Include(a => a.Teacher)
                .FirstOrDefaultAsync(a => a.Id == id);
        }

        public async Task<List<Appointment>> GetByStudentIdAsync(Guid studentId)
        {
            return await _context.Appointments
                .Include(a => a.Teacher)
                .Where(a => a.StudentId == studentId)
                .OrderByDescending(a => a.CreatedAt)
                .ToListAsync();
        }

        public async Task<List<Appointment>> GetByTeacherIdAsync(Guid teacherId)
        {
            return await _context.Appointments
                .Include(a => a.Student)
                .Where(a => a.TeacherId == teacherId)
                .OrderByDescending(a => a.CreatedAt)
                .ToListAsync();
        }

        public async Task<List<Appointment>> GetByStudentIdsAsync(List<Guid> studentIds)
        {
            return await _context.Appointments
                .Where(a => studentIds.Contains(a.StudentId))
                .Where(a => a.Status == AppointmentStatus.Pending)
                .ToListAsync();
        }

        public async Task<List<Appointment>> GetByIdsAsync(List<Guid> appointmentIds)
        {
            return await _context.Appointments.Where(a => appointmentIds.Contains(a.Id)).ToListAsync();
        }
        public async Task<List<Appointment>> GetByOrderIdAsync(Guid orderId)
        {
            return await _context.Appointments.Where(a => a.OrderId == orderId).ToListAsync();
        }
    }
} 